package Web;

import hibernate.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Hibernate;
import org.hibernate.classic.Session;

import basak.HibernateUtil;

public class signServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String e_mail = req.getParameter("mailAdress");
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");

		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();

			Query query = (Query) session
					.createQuery("Select u.e_mail from User u ");

			List<String> mailList = new ArrayList<String>();
			mailList = query.list();
			boolean newx = true;
			for (String x : mailList) {
				if (x.equals(e_mail))
					newx = false;
			}
			if (newx == true) {
				Query maxid = (Query) session
						.createQuery("Select max(u.id) from User u ");

				List<String> mid = new ArrayList<String>();
				mid = maxid.list();
				//int id=parseInt(mid.get(0));
				User newuser = new User();
				newuser.setId(7);
				newuser.setE_mail(e_mail);
				newuser.setPassword(password);
				newuser.setUser_name(userName);
				session.save(newuser);
				session.getTransaction().commit();
				req.getRequestDispatcher("/index.jsp").forward(req, resp);
			}
			else {
				req.setAttribute("ERROR_MSG", "Bu e-mail daha onceden kayitlidir !");
				req.getRequestDispatcher("/error.jsp").forward(req, resp);
			}
		} catch (Exception ex) {

		}

	}

}
